<?php

namespace app\company\controller;

use app\common\controller\CompanyController;
use app\common\service\annotation\ControllerAnnotation;
use app\common\service\annotation\NodeAnnotation;
use app\company\model\CompanyLog;
use think\App;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\response\Json;

/**
 * @ControllerAnnotation(title="日志")
 */
class Log extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model          = new CompanyLog();
        $this->relationSearch = true;
        $this->joinTable      = 'user';
    }

    /**
     * @NodeAnnotation (title="日志列表")
     * @return Json
     */
    public function list(): Json
    {
        [$limit, $where, $excludeFields] = $this->buildSearchParams(['month']);
        $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym');
        $model = $this->model->setMonth($month)->with('user')->where($where);
        try {
            $list = $model->where($this->defaultWhere)->order($this->sort)->paginate($limit);
        } catch (PDOException|DbException $exception) {
            $list = [];
        }
        return $this->success($list);
    }
}